
package com.xgry.bpm.api.base.userInfo.controller;

import com.xgry.bpm.common.utils.Result;;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import com.xgry.bpm.api.base.userInfo.service.UserInfoService;
import com.xgry.bpm.api.base.userInfo.entity. UserInfo;


/**
 * <p>
    *  前端控制器
    * </p>
 *
 * @author adrian
 * @since 2020-07-09
 * @version v1.0
 */
@RestController
@RequestMapping("/user-info")
public class UserInfoController {

    @Autowired
    private UserInfoService serInfoService;

    /**
     * 查询分页数据
     */
    @ApiOperation(value = "查询分页数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageIndex", value = "查询第几页", paramType = "query", required = true, dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "每页多少条", paramType = "query", required = true, dataType = "int"),
        @ApiImplicitParam(name = "userInfo", value = "客户查询参数", paramType = "query", required = false, dataType = "object")
    })
    @RequestMapping(value = "/getUserInfoListForPage", method= RequestMethod.POST)
    public Result getUserInfoListForPage(@RequestParam(name = "pageIndex", defaultValue = "1") int pageIndex,@RequestParam(name = "pageSize", defaultValue = "5") int pageSize , UserInfo userInfo){
        return Result.convertIpageToSuccess(serInfoService.page(new Page<>(pageIndex,pageSize),new QueryWrapper<UserInfo>(){{
            this.orderByDesc("pk_id");
            this.setEntity(userInfo);
        }}));
    }


    /**
    * 查询list数据
    */
    @ApiOperation(value = "查询list数据")
    @ApiImplicitParam(name = "查询数据", value = "查询数据", paramType = "query", required = false, dataType = "object")
    @RequestMapping(value = "/getUserInfoList", method= RequestMethod.POST)
    public Result getUserInfoList(UserInfo userInfo){
        return Result.success(HttpStatus.OK.getReasonPhrase(),serInfoService.list(new QueryWrapper<UserInfo>(){{
            this.orderByAsc("pk_id");
            this.setEntity(userInfo);
        }}));
    }



    /**
     * 新增
     */
    @ApiOperation(value = "新增数据")
    @ApiImplicitParam(name = "userInfojson", value = "新增数据json", paramType = "query", required = true, dataType = "String")
    @RequestMapping(value = "/addUserInfo", method = RequestMethod.POST)
    public Result addUserInfo(@RequestBody UserInfo userInfo){

        if (null == userInfo) {
            return Result.fail("Obj is null!");
        }

        if (serInfoService.save(userInfo)) {
            return Result.success();
        }

        return Result.fail();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据")
    @ApiImplicitParam(name="用户ids",value = "用户主键id Array",paramType = "delete",required = true,dataType = "int")
    @RequestMapping(value = "/deleteUserInfoByIds", method= RequestMethod.POST)
    public Result deleteUserInfo(@RequestParam("ids") List<Integer> ids){
        if (CollectionUtils.isEmpty(ids)) {
            return Result.fail("ids is null!");
        }

        if (serInfoService.removeByIds(ids)) {
            return Result.success();
        }

        return Result.fail();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新数据")
    @ApiImplicitParam(name="用户ids",value = "用户主键id Array",paramType = "update",required = true,dataType = "object")
    @RequestMapping(value = "/updateUserInfoById", method = RequestMethod.POST)
    public Result update(@RequestBody UserInfo userInfo){
        if (null == userInfo ||  0 == userInfo.getUserId().intValue()) {
            return Result.fail("obj is null!");
        }

        if (serInfoService.updateById(userInfo)) {
            return Result.success();
        }

        return Result.fail();
     }

}
